*ALTERNATIVE SAMPLING FOR ETHNORACIAL GROUP
* Makes a graph confirming what is discussed (not reported) in SI B.2.3 

clear all
use "ESS.dta" 


g D_raceNorel=D_race
replace D_raceNorel=(10-lwdscwp)/10 if D_raceNorel==. & rlgdnm>=5 & mocntr==1 & facntr==1 & Immigrant==0  

g D_raceNoimm=D_race
replace D_raceNoimm=(10-lwdscwp)/10  if  D_raceNoimm==. & Immigrant==1  

g D_raceNowest=D_race
replace D_raceNowest=(10-lwdscwp)/10 if  D_raceNowest==. &  Western==1  

  
drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"
 

  global y1 "D_raceNorel"
  global y2 "D_raceNoimm"
  global y3 "D_raceNowest"
  
 global x1 "total_eduyrs"
 global z1 "Age Female Edu_mum  i.Country  i.essround"
 global z2 "Female  Edu_mum i.Country  i.essround"
 global IV "T r"
 global z1b "Age Age2 Female Edu_mum Africa Asia Muslim Jew i.Country  i.essround"
 global z2b "Female  Edu_mum i.Country Africa Asia Muslim Jew  i.essround"



*PERCEIVED DISCRIMINATION
 
 
la var total_eduyrs "Years of education"

quietly: rdbwselect  $y1  r if Strong==1 ,  covs(Edu_mum Female  Africa Asia   r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )  
local ua =floor(e(h_mserd))
g w=max(0,`ua'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = T r)   $z2b  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store a
mat l e(first)
local Fa= floor(e(first)[rownumb(e(first),"SWF"),1])
local Na= e(N)
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meana=trim("`Mean'")
drop w

quietly: rdbwselect  $y2  r if Strong==1,  covs(Edu_mum Female Jew Muslim Africa Asia   r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )  
local ub =floor(e(h_mserd))
g w=max(0,`ub'-abs(r)) 

quietly: ivreg2  $y2  ($x1 = $IV)   $z2b  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store b
mat l e(first)
local Fb= floor(e(first)[rownumb(e(first),"SWF"),1])
local Nb= e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meanb=trim("`Mean'")
drop w


quietly: rdbwselect  $y3 r if Strong==1,  covs(Edu_mum Female Jew Muslim Africa Asia  r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )  
local uc =floor(e(h_mserd))
g w=max(0,`uc'-abs(r)) 

quietly: ivreg2  $y3  ($x1 = $IV)   $z2b  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store c
mat l e(first)
local Fc= floor(e(first)[rownumb(e(first),"SWF"),1])
local Nc= e(N)
su  $y3 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meanc=trim("`Mean'")
drop w


 	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color  "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
    grstyle set graphsize 13cm 12cm 	
	coefplot  (a, label("With religious" "{it:N}=`Na'") )   (b, label("With immigrants" "{it:N}=`Nb'" ))  (c, label("With  western" "{it:N}=`Nc'") ) ,     keep( total_eduyrs)   xscale(range(-0.05,-0.05)) xlab(-0.05(.02)0.05, grid gstyle(minor))   xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))  xtitle("Member of group discriminated against for ethnicity/race", size(medsmall))    ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels     title("{bf:Discriminatory attitudes}")  note( "With  religious: 1st stage {it:F}-stat = `Fa' | bandwidth = `ua' | Outcome mean = `Meana'." "With immigrants: 1st stage {it:F}-stat = `Fb' | bandwidth = `ub'." "With western: 1st stage {it:F}-stat = `Fc' | bandwidth = `uc'." , size(vsmall) span)

 
*********************************************************************************************************


clear all
use "ESS.dta" 


g PD_raceNorel=PD_race
replace PD_raceNorel=. if rlgdnm>=5 & mocntr==1 & facntr==1 & Immigrant==0 & Minority==0

g PD_raceNoimm=PD_race
replace PD_raceNoimm=. if Immigrant==1 & Jew==0 & Muslim==0 & Other==0 & Minority==0  & Africa==0 & Asia==0 & America==0 & mocntr==1 & facntr==1 

g PD_raceNowest=PD_race
replace PD_raceNowest=. if Jew==0 & Muslim==0 & Other==0  & Minority==0  & Western==1 & Africa==0 & Asia==0 & America==0  & mocntr==1 & mocntr==1 & facntr==1 


  global y1 "PD_raceNorel"
  global y2 "PD_raceNoimm"
  global y3 "PD_raceNowest"
  
 global x1 "total_eduyrs"
 global z1 "Age Female Edu_mum  i.Country  i.essround"
 global z2 "Female  Edu_mum i.Country  i.essround"
 global z1b "Age Age2 Female Edu_mum Africa Asia Muslim Jew i.Country  i.essround"
 global z2b "Female  Edu_mum i.Country Africa Asia Muslim Jew  i.essround"



*PERCEIVED DISCRIMINATION
 
 
la var total_eduyrs "Years of education"

 

quietly: rdbwselect  $y1  r if Strong==1 ,  covs(Edu_mum Female  Africa Asia   r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )  
local ua =floor(e(h_mserd))
g w=max(0,`ua'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = $IV)   $z2b  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store a
mat l e(first)
local Fa= floor(e(first)[rownumb(e(first),"SWF"),1])
local Na= e(N)
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meana=trim("`Mean'")
drop w

quietly: rdbwselect  $y2  r if Strong==1,  covs(Edu_mum Female Jew Muslim Africa Asia   r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )  
local ub =floor(e(h_mserd))
g w=max(0,`ub'-abs(r)) 

quietly: ivreg2  $y2  ($x1 = $IV)   $z2b  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store b
mat l e(first)
local Fb= floor(e(first)[rownumb(e(first),"SWF"),1])
local Nb= e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meanb=trim("`Mean'")
drop w


quietly: rdbwselect  $y3 r if Strong==1,  covs(Edu_mum Female Jew Muslim Africa Asia  r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )  
local uc =floor(e(h_mserd))
g w=max(0,`uc'-abs(r)) 

quietly: ivreg2  $y3  ($x1 = $IV)   $z2b  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store c
mat l e(first)
local Fc= floor(e(first)[rownumb(e(first),"SWF"),1])
local Nc= e(N)
su  $y3 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meanc=trim("`Mean'")
drop w


 	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color  "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
    grstyle set graphsize 13cm 12cm 	
	coefplot  (a, label("Without religious" "{it:N}=`Na'") )   (b, label("Without immigrants" "{it:N}=`Nb'" ))  (c, label("Without western" "{it:N}=`Nc'") ) ,     keep( total_eduyrs)   xscale(range(-0.05,-0.05)) xlab(-0.05(.02)0.05, grid gstyle(minor))   xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))  xtitle("Member of group discriminated against for ethnicity/race", size(medsmall))    ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels     title("{bf:Perceived discrimination}")  note( "Without religious: 1st stage {it:F}-stat = `Fa' | bandwidth = `ua' | Outcome mean = `Meana'." "Without immigrants: 1st stage {it:F}-stat = `Fb' | bandwidth = `ub'." "Without western: 1st stage {it:F}-stat = `Fc' | bandwidth = `uc'." , size(vsmall) span)
 
 

  
 
	 
	 
